//var Hobbies = mongoose.model('masterData_Hobbies');
var Regions = mongoose.model('masterData_Regions');
var Dictionary = mongoose.model('masterData_Dictionaries');
var Users = mongoose.model('user');
var userPortrait = mongoose.model('masterData_UserPortrait');


////获取兴趣列表
//exports.getHobbyListService = function (callback) {
//    Hobbies.find({}, '_id hobbyName').exec(callback);
//};

//获取国家列表
exports.getCountryListService = function (callback) {
    Regions.find({'type': 'country'}, '_id name areaCode').exec(callback);
};

//获取某个国家下所有省
exports.getProvinceListByCountyIdService = function (countryId, callback) {
    Regions.find({'type': 'province', 'parentId': countryId}, '_id name areaCode').exec(callback);
};

//获取某个省下所有市
exports.getCityListByProvinceIdService = function (provinceId, callback) {
    Regions.find({'type': 'city', 'parentId': provinceId}, '_id name areaCode').exec(callback);
};

//获取某个市下所有县
exports.getCountyListByCityIdService = function (cityId, callback) {
    Regions.find({'type': 'county', 'parentId': cityId}, '_id name areaCode').exec(callback);
};

//获取某个国家、省、市、县的信息
exports.getRegionByIdService = function (regionId, callback) {
    Regions.findOne({'_id': regionId}, '_id name areaCode type cultureCode').exec(callback);
};

//获取地区列表
exports.getRegionListService = function (date, callback) {
    Regions.findOne({}, {}, function (err, results) {
        if (results != null && results._doc.dateLastModified > new Date(date)) {
            Regions.find({}, '_id type name parentId cultureCode areaCode dateLastModified').exec(callback);
        } else {
            callback(null, null);
        }
    });
}

//获取数据字典
exports.getDataDictionaryService = function (date, callback) {
    Dictionary.findOne({}, {}, function (err, results) {
        if (results._doc.dateLastModified > new Date(date)) {
            Dictionary.find({
                $or: [{'category': {$ne: '02'}}, {
                    'category': '02',
                    'displayOrder': {$ne: null}
                }]
            }, '_id category value defaultURL imageURL dateLastModified').sort('displayOrder').exec(function (err, result) {
                callback(err, result);
            });
        } else {
            callback(null, null);
        }
    });
};

//根据类别获取数据字典
exports.getDataDictionaryByCategoryService = function (category, callback) {
    Dictionary.find({category: category, $and: [{value: {$ne: '全部'}}, {value: {$ne: '推荐'}}]}).sort('displayOrder')
        .exec(function (err, results) {
            if (results == undefined || results == null) {
                var msg = {
                    "message": 'get err'
                };
                callback(msg, null);
            } else {
                callback(null, results);
            }
        })
};

exports.getUserPortraitService = function (date, callback) {
    userPortrait.findOne({}, {}, function (err, results) {
        if (results._doc.dateCreated > new Date(date)) {
            userPortrait.find({}, '_id value portraitURL gender dateCreated').sort('displayOrder').exec(callback);
        } else {
            callback(null, null);
        }
    });
};


/**
 * 1.6.0 主数据（兴趣爱好）
 * @param ts
 * @param callback
 */
exports.userHobbyService = function (ts, callback) {
    var date = new Date(Number(ts));

    Dictionary.find({
        category: '01',
        dateLastModified: {$gt: date}
    }).sort('-dateLastModified').limit(1).exec(function (err, result) {
        if (result != null && result.length > 0) {
            Dictionary.find({'category': '01'}, '_id category value defaultURL imageURL dateLastModified').sort('displayOrder').exec(function (err, re) {
                var data = {
                    v: result[0].dateLastModified.getTime(),
                    hobbies: re
                };
                callback(null, data)
            });

        } else {
            Dictionary.find({category: '01'}).sort('-dateLastModified').limit(1).exec(function (err, result) {
                var data = {
                    v: result[0].dateLastModified.getTime(),
                    hobbies: []
                };
                callback(null, data)
            })
        }
    })
};


/**
 * 1.6.0 主数据（用户肖像）
 * @param ts
 * @param callback
 */
exports.userPortraitService = function (ts, callback) {
    var date = new Date(Number(ts));

    userPortrait.find({dateCreated: {$gt: date}}).sort('-dateCreated').limit(1).exec(function (err, result) {
        if (result != null && result.length > 0) {
            userPortrait.find({}, '_id value portraitURL gender dateCreated').sort('displayOrder').exec(function (err, re) {
                var data = {
                    v: result[0].dateCreated.getTime(),
                    portraits: re
                };
                callback(null, data)
            });
        } else {
            userPortrait.find().sort('-dateCreated').limit(1).exec(function (err, result) {
                var data = {
                    v: result[0].dateCreated.getTime(),
                    portraits: []
                };
                callback(null, data)
            })
        }
    })
};

exports.regionListService = function (ts, vcode, callback) {

    var v = 1;
    if (Number(ts) < v && vcode >= 300000) {
        Regions.find({$or: [{type: 'province'}, {type: 'city'}]}).sort({dateCreated: 1}).exec(function (err, result) {
            var data = {
                v: v,
                regions: result
            };
            callback(null, data)
        })
    } else {
        var data = {
            v: v,
            regions: []
        };
        callback(null, data)
    }

};

exports.getTagsService = function (version, callback) {
    Dictionary.find({
        category: '02',
        level: 1
    }, '_id category value dateLastModified').sort('-displayOrder').exec(function (err, result) {
        var data = {
            v: 1,
            tags: result
        };
        callback(null, data)
    })
};

exports.cityListService = function (ts, callback) {

    var v = 1;
    if (Number(ts) < v) {
        Regions.find({type: 'city'}, 'name pinyin').sort({pinyin: 1, name: 1}).exec(function (err, result) {
            result.forEach(function (item) {
                item._doc.initial = item.pinyin.substr(0, 1).toUpperCase();
            });
            var data = {
                v: v,
                list: result
            };
            callback(err, data)
        });
    } else {
        var data = {
            v: v,
            list: []
        };
        callback(null, data)
    }

};